package cn.iocoder.yudao.module.system.service.authrecorddatascopevalue;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.system.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.authrecorddatascopevalue.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.authrecorddatascopevalue.AuthRecordDataScopeValueDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.authrecorddatascopevalue.AuthRecordDataScopeValueMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 授权记录和权限范围值关系 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AuthRecordDataScopeValueServiceImpl implements AuthRecordDataScopeValueService {

    @Resource
    private AuthRecordDataScopeValueMapper authRecordDataScopeValueMapper;

    @Override
    public Long createAuthRecordDataScopeValue(AuthRecordDataScopeValueSaveReqVO createReqVO) {
        // 插入
        AuthRecordDataScopeValueDO authRecordDataScopeValue = BeanUtils.toBean(createReqVO, AuthRecordDataScopeValueDO.class);
        authRecordDataScopeValueMapper.insert(authRecordDataScopeValue);
        // 返回
        return authRecordDataScopeValue.getId();
    }

    @Override
    public void updateAuthRecordDataScopeValue(AuthRecordDataScopeValueSaveReqVO updateReqVO) {
        // 校验存在
        validateAuthRecordDataScopeValueExists(updateReqVO.getId());
        // 更新
        AuthRecordDataScopeValueDO updateObj = BeanUtils.toBean(updateReqVO, AuthRecordDataScopeValueDO.class);
        authRecordDataScopeValueMapper.updateById(updateObj);
    }

    @Override
    public void deleteAuthRecordDataScopeValue(Long id) {
        // 校验存在
        validateAuthRecordDataScopeValueExists(id);
        // 删除
        authRecordDataScopeValueMapper.deleteById(id);
    }

    private void validateAuthRecordDataScopeValueExists(Long id) {
        if (authRecordDataScopeValueMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.AUTH_RECORD_DATA_SCOPE_VALUE_NOT_EXISTS);
        }
    }

    @Override
    public AuthRecordDataScopeValueDO getAuthRecordDataScopeValue(Long id) {
        return authRecordDataScopeValueMapper.selectById(id);
    }

    @Override
    public PageResult<AuthRecordDataScopeValueDO> getAuthRecordDataScopeValuePage(AuthRecordDataScopeValuePageReqVO pageReqVO) {
        return authRecordDataScopeValueMapper.selectPage(pageReqVO);
    }

}